package com.house.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.User;
import com.house.common.core.page.TableDataInfo;
import com.house.common.exception.ServiceException;
import com.house.system.service.IHouseService;
import com.house.system.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.Quit;
import com.house.system.service.IQuitService;

/**
 * 退租对象Controller
 *
 * @author tc
 * @date 2022-02-25
 */
@RestController
@RequestMapping("/domain/quit")
public class QuitController extends BaseController {
    @Autowired
    private IQuitService quitService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IHouseService houseService;

    /**
     * 查询退租对象列表
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:list')")
    @GetMapping("/list")
    public TableDataInfo list(Quit quit) {
        startPage();
        List<Quit> list = quitService.selectQuitList(quit);
        return getDataTable(list);
    }

    /**
     * 获取退租对象详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:query')")
    @GetMapping(value = "/{quitId}")
    public AjaxResult getInfo(@PathVariable("quitId") Long quitId) {
        return AjaxResult.success(quitService.selectQuitByQuitId(quitId));
    }

    /**
     * 新增退租对象
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Quit quit) {
        return toAjax(quitService.insertQuit(quit));
    }

    /**
     * 用户申请退租
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:add')")
    @PostMapping("/userAddQuit")
    public AjaxResult userAddQuit(@RequestBody Quit quit) {
        quit.setUserId(getUserId());
        quit.setStatus("0");  //未处理
        //判断是否重复提交
        List<Quit> quits = quitService.selectQuitList(quit);
        if (quits.size() > 0) {
            //说明申请过,不需要重复申请
            throw new ServiceException("请勿重复提交,等待中介处理", 400);
        } else {
            return toAjax(quitService.insertQuit(quit));
        }
    }

    /**
     * 修改退租对象
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Quit quit) {
        return toAjax(quitService.updateQuit(quit));
    }

    /**
     * 删除退租对象
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:remove')")
    @DeleteMapping("/{quitIds}")
    public AjaxResult remove(@PathVariable Long[] quitIds) {
        return toAjax(quitService.deleteQuitByQuitIds(quitIds));
    }


    /**
     * 获取退租对象详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:query')")
    @GetMapping("/detail/{quitId}/{houseId}/{userId}")
    public AjaxResult getDetail(@PathVariable("quitId") Long quitId, @PathVariable("houseId") Long houseId, @PathVariable("userId") Long userId) {
        AjaxResult ajax = AjaxResult.success();
        House house = houseService.selectHouseByHouseId(houseId);
        User user = userService.selectUserByUserId(userId);
        Quit q = quitService.selectQuitByQuitId(quitId);
        ajax.put("house", house);
        ajax.put("user", user);
        ajax.put("quit", q);
        return ajax;
    }

    /**
     * 中介处理退租对象
     */
    @PreAuthorize("@ss.hasPermi('domain:quit:handle')")
    @PutMapping("/handle/{quit}/{houseId}")
    public AjaxResult handleQuit(@PathVariable("quit") Long quitId, @PathVariable("houseId") Long houseId) {
        return toAjax(quitService.handleQuit(quitId, houseId));
    }
}
